package com.java2007.hotel.dao.impl;

import com.java2007.hotel.dao.OrderDao;
import com.java2007.hotel.pojo.Order;
import com.java2007.hotel.utils.JdbcUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.SQLException;
import java.util.Date;
import java.util.List;

/**
 * 订单dao实体类
 * @author csl
 * @version V1.0
 * @date 2021/01/23
 **/
public class OrderDaoImpl implements OrderDao {

    /**
     * 查询所有订单
     * @return
     */
    @Override
    public List<Order> selectAll() throws SQLException {
        QueryRunner queryRunner= new QueryRunner(JdbcUtils.getDataSource());
        String sql = "SELECT\n" +
                "  order_id id,\n" +
                "  order_number number,\n" +
                "  order_table_id tableId,\n" +
                "  order_total_amount totalAmount,\n" +
                "  order_status status ,\n" +
                "  order_create_date createDate,\n" +
                "  order_update_date updateDate,\n" +
                "  order_creater_id createrId\n" +
                "FROM t_order";
        List<Order> query = queryRunner.query(sql, new BeanListHandler<Order>(Order.class));
        return query;
    }
    

    /**
     * 修改订单
     * @return
     */
    @Override
    public int updateOrderStatus(byte status, int id) throws SQLException {
        QueryRunner queryRunner= new QueryRunner(JdbcUtils.getDataSource());
        Date date = new Date();
        String sql = "\n" +
                "UPDATE t_order SET order_status = ? ,order_update_date = ? WHERE order_id =?";
        int update = queryRunner.update(JdbcUtils.getConnection(), sql, status,date, id);
        return update;
    }

    /**
     * 根据桌子id查询订单
     * @param tableId
     * @return
     * @throws SQLException
     */
    @Override
    public List<Order> selectByTableId(int tableId) throws SQLException {
        QueryRunner queryRunner= new QueryRunner(JdbcUtils.getDataSource());
        String sql = "SELECT\n" +
                "  order_id id,\n" +
                "  order_number number,\n" +
                "  order_table_id tableId,\n" +
                "  order_total_amount totalAmount,\n" +
                "  order_status status,\n" +
                "  order_create_date createDate,\n" +
                "  order_update_date updateDate,\n" +
                "  order_creater_id createrId\n" +
                "FROM t_order\n" +
                "WHERE order_table_id = ?";
        List<Order> query = queryRunner.query(sql, new BeanListHandler<Order>(Order.class),tableId);
        return query;
    }
    /**
     * 根据桌子id订单
     * @param id
     * @return
     * @throws SQLException
     */
    @Override
    public Order selectById(int id) throws SQLException {
        QueryRunner queryRunner= new QueryRunner(JdbcUtils.getDataSource());
        String sql = "SELECT\n" +
                "  order_id id,\n" +
                "  order_number number,\n" +
                "  order_table_id tableId,\n" +
                "  order_total_amount totalAmount,\n" +
                "  order_status status,\n" +
                "  order_create_date createDate,\n" +
                "  order_update_date updateDate,\n" +
                "  order_creater_id createrId\n" +
                "FROM t_order\n" +
                "WHERE order_id = ?";
        Order query = queryRunner.query(sql, new BeanHandler<>(Order.class),id);
        return query;
    }


    /**
     * 增加一个订单
     * @param order
     * @return
     */
    @Override
    public int addOrder(Order order) throws SQLException {
        String sql = "INSERT INTO t_order\n" +
                "            (order_number,\n" +
                "             order_table_id,\n" +
                "             order_total_amount,\n" +
                "             order_status,\n" +
                "             order_create_date,\n" +
                "             order_update_date,\n" +
                "             order_creater_id)\n" +
                "VALUES (?,?,?,?,?,?,?)";
        QueryRunner queryRunner= new QueryRunner(JdbcUtils.getDataSource());
        int update = queryRunner.update(JdbcUtils.getConnection(), sql, order.getNumber(), order.getTableId(),
                order.getTotalAmount(), order.getStatus(), order.getCreateDate(), order.getUpdateDate(), order.getCreaterId());
        return update;
    }
    /**
     * 删除一个订单
     * @param id
     * @return
     */
    @Override
    public int delete(int id) throws SQLException {
        QueryRunner queryRunner= new QueryRunner(JdbcUtils.getDataSource());
        String sql ="DELETE FROM t_order  WHERE order_id = ?";
        int update = queryRunner.update(JdbcUtils.getConnection(), sql, id);
        return update;
    }


    /**
     * 根据订单编号修改订单
     * @return
     */

    @Override
    public int updateOrderStatusByNumber(byte status, String number) throws SQLException {
        QueryRunner queryRunner= new QueryRunner(JdbcUtils.getDataSource());
        Date date = new Date();
        String sql = "\n" +
                "UPDATE t_order SET order_status = ? ,order_update_date = ? WHERE order_number =?";
        int update = queryRunner.update(JdbcUtils.getConnection(), sql, status,date, number);
        return update;
    }

    /**
     * 根据订单查询订单
     * @param number
     * @return
     * @throws SQLException
     */
    @Override
    public Order selectByByNumber(String number) throws SQLException {
        QueryRunner queryRunner= new QueryRunner(JdbcUtils.getDataSource());
        String sql = "SELECT\n" +
                "  order_id id,\n" +
                "  order_number number,\n" +
                "  order_table_id tableId,\n" +
                "  order_total_amount totalAmount,\n" +
                "  order_status status,\n" +
                "  order_create_date createDate,\n" +
                "  order_update_date updateDate,\n" +
                "  order_creater_id createrId\n" +
                "FROM t_order\n" +
                "WHERE order_number = ?";
        Order query = queryRunner.query(sql, new BeanHandler<>(Order.class),number);
        return query;
    }
}
